<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use App\Models\AttendanceStatistics;
use Modules\Manager\Entities\Manager;
use App\Http\Controllers\Wx\SendTemplateController;
use Modules\Car\Entities\CarOrder;

class MonthOrderTip implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $order;
    public function __construct()
    {
        //
       // $this->order = $order;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $datalist = CarOrder::query()->where('status',5)->where('process_status',2)->where('pre_lease_days','>=',30)->whereRaw("DATEDIFF(NOW(), STR_TO_DATE(pre_fetch_time, '%Y-%m-%d')) >= 30")->get();
        if(count($datalist)>0){
            foreach ($datalist as $item){
                // 找到订单的所属部门ID对应的店长，发送提醒
                $useritem = Manager::query()->where('post_id', 2)->where('department_id', $item->department)->first();
                if($useritem&&$useritem->wechat_openid){
                    $msg = [
                        'openid'=>$useritem->wechat_openid,
                        'order_sn'=>$item->order_sn,
                        'wx_name'=>$item->wx_name,
                    ];
                    (new SendTemplateController())->sendTemplateYzdd($msg);
                    info(json_encode($msg));
                }
            }
        }
    }
}
